package com.zq;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ybxy6
 * @Titile: MapPrictice
 * @Author: zqsfg
 * @Description: TODO
 */
public class MapPrictice {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("小张","A");
        map.put("小李","B");
        map.put("小王","C");
        map.put("小赵","D");
        map.put("1","B");
        map.put("小2","C");
        map.put("小3","A");
        map.put("小4","B");
        map.put("小5","C");
        map.put("小6","A");
        map.put("小7","B");
        map.put("小8","C");
        map.put("小9","C");
        map.put("小10","C");
        map.put("小11","C");
        map.put("小12","C");
        map.put("小13","C");
        //调用方法
        count(map);
        System.out.println("----------");
        pricticeCount1(map);


    }
    public static void count(Map<String, String>map){
        /*
        * 1.创建新的map集合，用来存储统计投票结果
        * 2.遍历map集合，获取每一个键值对
        * ---1.将value拿出来，将value（A）当做key纯粹到新的集合
        * ---2.如果A不存在，则将value（A）作为value，1作为value，放入新的集合中
        * ---3.如果A存在，,那么将A对应值加一
        *3.遍历新map集合，打印结果
        *
        * */
        Map<String, Integer> newMap = new HashMap<>();
       map.forEach((k,v)->{
           boolean c=newMap.containsKey(v);
           if (c){//存在
               //将老的值取出来加一，重新存进去
               Integer old= newMap.get(v);
               newMap.put(v,old+1);
           }else {//不存在
               newMap.put(v,1);
           }

       });
       //打印出来
       newMap.forEach((k,v)->{
           System.out.println(k+"------>"+v);
       });
    }

    public static void pricticeCount1(Map<String, String>map){
        Map<String, Integer> newMap = new HashMap<>();
        map.forEach((k,v)->{
            boolean c=newMap.containsKey(v);
            if (c){ //存在get(v)获取键
                /*Integer old= newMap.get(v);
                newMap.put(v,old+1);*/
                newMap.compute(v, (key, old) -> old + 1);

            }else {//不存在
                newMap.put(v,1);

            }
        });
        newMap.forEach((k,v)->{
            System.out.println(k+"------>"+v);
        });
    }
}
